import 'package:get/get.dart';
import '../models/user.dart';
import '../services/user_service.dart';
import '../../../core/state/page_state.dart';
import '../../../core/network/api_client.dart';

class NetworkDemoController extends GetxController {
  final UserService _userService;

  NetworkDemoController(this._userService);

  // 响应式状态
  final Rx<PageState> _pageState = PageState.loading.obs;
  final RxList<User> _users = <User>[].obs;
  final RxString _errorMessage = ''.obs;
  final RxBool _isRefreshing = false.obs;
  final RxBool _useRealApi = false.obs;

  // Getters
  PageState get pageState => _pageState.value;
  List<User> get users => _users;
  String get errorMessage => _errorMessage.value;
  bool get isRefreshing => _isRefreshing.value;
  bool get useRealApi => _useRealApi.value;

  @override
  void onInit() {
    super.onInit();
    loadUsers();
  }

  Future<void> loadUsers() async {
    try {
      _pageState.value = PageState.loading;
      _errorMessage.value = '';
      
      final users = await _userService.getUsers();
      
      if (users.isEmpty) {
        _pageState.value = PageState.empty;
      } else {
        _users.assignAll(users);
        _pageState.value = PageState.success;
      }
    } catch (error) {
      _pageState.value = ApiClient.handleError(error);
      _errorMessage.value = ApiClient.getErrorMessage(error);
    }
  }

  Future<void> refreshUsers() async {
    try {
      _isRefreshing.value = true;
      
      final users = await _userService.getUsers();
      
      if (users.isEmpty) {
        _pageState.value = PageState.empty;
      } else {
        _users.assignAll(users);
        _pageState.value = PageState.success;
      }
      
      Get.snackbar(
        '刷新成功',
        '用户数据已更新',
        snackPosition: SnackPosition.BOTTOM,
        duration: const Duration(seconds: 2),
      );
    } catch (error) {
      _pageState.value = ApiClient.handleError(error);
      _errorMessage.value = ApiClient.getErrorMessage(error);
      
      Get.snackbar(
        '刷新失败',
        ApiClient.getErrorMessage(error),
        snackPosition: SnackPosition.BOTTOM,
        duration: const Duration(seconds: 3),
      );
    } finally {
      _isRefreshing.value = false;
    }
  }

  void retryLoad() {
    loadUsers();
  }

  void toggleApiMode() {
    _useRealApi.value = !_useRealApi.value;
    
    Get.snackbar(
      'API 模式切换',
      _useRealApi.value ? '已切换到真实 API' : '已切换到 Mock 数据',
      snackPosition: SnackPosition.BOTTOM,
      duration: const Duration(seconds: 2),
    );
    
    // 重新加载数据
    loadUsers();
  }

  Future<void> simulateNetworkError() async {
    _pageState.value = PageState.networkError;
    _errorMessage.value = '模拟网络连接失败';
    
    Get.snackbar(
      '网络错误模拟',
      '已模拟网络连接失败状态',
      snackPosition: SnackPosition.BOTTOM,
      duration: const Duration(seconds: 2),
    );
  }

  Future<void> simulatePermissionError() async {
    _pageState.value = PageState.permissionError;
    _errorMessage.value = '模拟权限不足';
    
    Get.snackbar(
      '权限错误模拟',
      '已模拟权限不足状态',
      snackPosition: SnackPosition.BOTTOM,
      duration: const Duration(seconds: 2),
    );
  }
}